import akshare as ak
import pandas as pd
import time
import os
from threading import Timer
from common import utils

class FlowWatcher:
    def __init__(self, setFlow):
        self.setFlow = setFlow

    def pull_once(self):
        df = ak.stock_individual_fund_flow_rank(indicator="今日")

        for index, row in df.iterrows():
            code = row['代码']

            if (utils.isBadStock(code)):
                continue

            #3self.allShares[code] = {
            #3    'sbig' : row['今日超大单净流入-净额']
            #3}
            self.setFlow(code, row['今日超大单净流入-净额'])

    def timer_entry(self):
        start = time.time()
        self.pull_once()
        dt = time.time() - start
        print("资金流入更新耗时 %.2f 秒" %(dt))
        tr = Timer(utils.getNextTime(), self.timer_entry)
        tr.start()

    def start(self):
        tr = Timer(1, self.timer_entry)
        tr.start()

    def getByCode(self, code):
        return self.allShares[code]
